package com.kermel.andromeda.data;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

import com.kermel.common.util.DateTimeUtils;
import com.kermel.common.util.StringUtils;

public class CountdownTimer extends Alarm {
    
    public WhenCountdownTimerEndsChoice whenCountdownTimerEndsChoice;
    public int hoursFromNow;
    public int minutesFromNow;
    
    public CountdownTimer() {
    }
    
    public long calculateTriggerTime() {
        long hoursMS = DateTimeUtils.hoursToMilliseconds(hoursFromNow);
        long minutesMS = DateTimeUtils.minutesToMilliseconds(minutesFromNow);
        
        triggerTime = hoursMS + minutesMS;
        
        return triggerTime;
    }

    // ================================================================================================================
    // Parcel-related items
    public static final String COUNTDOWN_TIMER_PARCEL_NAME = "countdownTimer";
    public static final String WHEN_COUNTDOWN_TIMER_ENDS_CHOICE_KEY = "whenTimerEndsChoice";
    public static final String PARCEL_HOURS_FROM_NOW = "hoursFromNow";
    public static final String PARCEL_MINUTES_FROM_NOW = "minutesFromNow";
    
    public CountdownTimer(Parcel source) {
        super(source);
        
        Bundle bundle = source.readBundle();
        
        if (bundle != null) {
            whenCountdownTimerEndsChoice = retrieveWhenCountdownTimerEndsChoiceFromBundle(bundle);
            hoursFromNow = bundle.getInt(PARCEL_HOURS_FROM_NOW);
            minutesFromNow = bundle.getInt(PARCEL_MINUTES_FROM_NOW);
        }
    }
    
    private WhenCountdownTimerEndsChoice retrieveWhenCountdownTimerEndsChoiceFromBundle(Bundle bundle) {
        WhenCountdownTimerEndsChoice choice = WhenCountdownTimerEndsChoice.UNKNOWN;
        
        try {
            String bundleChoiceString = bundle.getString(WHEN_COUNTDOWN_TIMER_ENDS_CHOICE_KEY);
            
            if (StringUtils.isNotBlank(bundleChoiceString))
                choice = WhenCountdownTimerEndsChoice.valueOf(bundleChoiceString);
            
        } catch (Exception e) {
            System.err.println(String.format("CountdownTimer.retrieveWhenCountdownTimerEndsChoiceFromBundle - Error: %s", e.getMessage()));
            e.printStackTrace();
        }
        
        return choice;
    }
    
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        
        Bundle bundle = new Bundle();
        
        if (whenCountdownTimerEndsChoice != null)
            bundle.putString(WHEN_COUNTDOWN_TIMER_ENDS_CHOICE_KEY, whenCountdownTimerEndsChoice.name());
        
        bundle.putInt(PARCEL_HOURS_FROM_NOW, hoursFromNow);
        bundle.putInt(PARCEL_MINUTES_FROM_NOW, minutesFromNow);
        
        dest.writeBundle(bundle);
    }
    
    public static final Parcelable.Creator<CountdownTimer> CREATOR = new Parcelable.Creator<CountdownTimer>() {
        public CountdownTimer createFromParcel(Parcel source) {
            return new CountdownTimer(source);
        }
        
        public CountdownTimer[] newArray(int size) {
            return new CountdownTimer[size];
        }
    };
}
